import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GameObjectList<YXY> {
    public YXY t;
    //我们可以使用泛型来定义一个通用的数据结构，例如一个列表。
    // 以王者荣耀为例，我们可以创建一个泛型类`GameObjectList`，用于存储游戏对象（英雄、炮台、野怪等）

    private List<YXY> gameObjects;

    {
        gameObjects = new ArrayList<>();
    }

    public void add(YXY t) {
        gameObjects.add(t);
    }

    public void remove(YXY t) {
        gameObjects.remove(t);
    }

    public int size() {
        return gameObjects.size();
    }

    //但不可以在静态方法中使用类的泛型
    //⑥ 异常类不能是带泛型的。
    public /*static*/  YXY get(int index) {
        return gameObjects.get(index);
    }

    static class Hero {
        //英雄名
        private String name;
        // 战力
        private int combat;

        public Hero(String name, int combat) {
            this.name = name;
            this.combat = combat;
        }


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getCombat() {
            return combat;
        }

        public void setCombat(int combat) {
            this.combat = combat;
        }

        @Override
        public String toString() {
            return "Hero{" +
                    "name='" + name + '\'' +
                    ", combat=" + combat +
                    '}';
        }
    }

    static class Turret {
        //炮台名
        private String name;
        // 血量
        private int blood;

        public Turret(String name, int blood) {
            this.name = name;
            this.blood = blood;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getBlood() {
            return blood;
        }

        public void setBlood(int blood) {
            this.blood = blood;
        }

        @Override
        public String toString() {
            return "Turret{" +
                    "name='" + name + '\'' +
                    ", blood=" + blood +
                    '}';
        }
    }

    static class Monster {
        //怪物名
        private String name;
        // 经济
        private int economic;

        public Monster(String name, int economic) {
            this.name = name;
            this.economic = economic;
        }

        public int getEconomic() {
            return economic;
        }

        public void setEconomic(int economic) {
            this.economic = economic;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Monster{" +
                    "name='" + name + '\'' +
                    ", economic=" + economic +
                    '}';
        }
    }

    public static void main(String[] args) {
        GameObjectList<Hero> heroList = new GameObjectList<>();
        heroList.add(new Hero("孙尚香", 1000));
        heroList.add(new Hero("鲁班", 8000));
        Hero hero = heroList.get(0);

//        heroList.add(new Monster("蓝霸符", 200));//因为heroList泛型是Hero，所以不能添加Monster
        GameObjectList<Monster> monsterList = new GameObjectList<>();
        monsterList.add(new Monster("蓝霸符", 200));
        monsterList.add(new Monster("大龙", 500));
        monsterList.add(new Monster("红霸符", 500));
        GameObjectList<Turret> turretList = new GameObjectList<>();
        turretList.add(new Turret("中二塔", 100));
        turretList.add(new Turret("大水晶", 20000));

        GameObjectList list = new GameObjectList<>();
//        GameObjectList<Object> list = new GameObjectList<>();
        list.add(new Turret("中二塔", 100));
        list.add(new Hero("鲁班", 8000));

        System.out.println(Arrays.toString(list.gameObjects.toArray()));


    }

}


